package com.yxhl.zoume.core.user.presenter.mywallet;

import android.content.Context;

import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.user.view.IWalletBalanceView;
import com.yxhl.zoume.data.http.model.mywallet.WalletBalanceBean;
import com.yxhl.zoume.data.http.rest.param.base.BaseRequestParam;
import com.yxhl.zoume.data.http.rest.response.mywallet.WalletBalanceResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.mywallet.WalletBalanceUseCase;

import javax.inject.Inject;

/**
 * 项目名称：Ferrari
 * Created by menggod on 2016/11/20.
 */

public class WalletBalancePresenter extends BasePresenter implements IPresenter {
    IWalletBalanceView mIWalletBalanceView;
    @Inject
    WalletBalanceUseCase mWalletBalanceUseCase;

    @Inject
    public WalletBalancePresenter(Context context) {
        super(context);
    }

    public void queryWalletBalance() {
        BaseRequestParam param = new BaseRequestParam();
        param.setMobile(getMobile())
                .setToken(getToken());
        mWalletBalanceUseCase.execute(param,new ZMSubscriber<WalletBalanceResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mIWalletBalanceView.renderWalletBalanceOnError(e);
            }

            @Override
            public void onNext(WalletBalanceResponse walletBalanceResponse) {
                super.onNext(walletBalanceResponse);
                if (null != walletBalanceResponse) {
                    boolean isSuccess = walletBalanceResponse.isSucc();
                    if (isSuccess) {
                        WalletBalanceBean walletBalanceBean = walletBalanceResponse.getWalletBalanceBean();
                        mIWalletBalanceView.renderWalletBalanceOnSuccess(walletBalanceBean);
                    } else {
                        mIWalletBalanceView.renderWalletBalanceOnFailure(walletBalanceResponse.getResultMessage());
                    }
                }
            }

        });

    }

    @Override
    public void attachView(BaseView view) {
        mIWalletBalanceView = (IWalletBalanceView) view;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWalletBalanceUseCase.unsubscribe();
    }
}
